﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ThirdPartyNinjas.XnaUtility;

namespace ElementumForever.Factory
{
    class ContentFactory:IDisposable
    {
        private static ContentManager contain;
        private static GraphicsDevice graphicsDevice;
        static Dictionary<string, Texture2D> textures;
        static Dictionary<string, TextureAtlas> textureAtlaes;


        public static void Initialize(MainGame game)
        {
            ContentFactory.contain = game.Content;
            ContentFactory.graphicsDevice = game.GraphicsDevice;
            textureAtlaes = new Dictionary<string, TextureAtlas>();
            textures = new Dictionary<string, Texture2D>();
        }

        public static Texture2D LoadTexture2D(string assetName)
        {
            if (textures.ContainsKey(assetName))
                return textures[assetName];
            else
            {
                Texture2D texture2D = contain.Load<Texture2D>(assetName);
                textures.Add(assetName, texture2D);
                return texture2D;
            }
        }

        public static TextureAtlas LoadTextureAtlas(string assetName)
        {
            if (textureAtlaes.ContainsKey(assetName))
                return textureAtlaes[assetName];
            else
            {
                TextureAtlas textureAtlas = contain.Load<TextureAtlas>(assetName);
                textureAtlaes.Add(assetName, textureAtlas);
                return textureAtlas;
            }
        }

        public static XmlTexture LoadXmlTexture(string assetName)
        {
            return new XmlTexture(assetName);
        }

        public void Dispose()
        {
            textures.Clear();
            textureAtlaes.Clear();
            contain.Unload();
        }
    }
}
